using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using Microsoft.Xna.Framework;

namespace WPCoco.Tasks
{
    public class FloatAnimation : TargetedTask<object>
    {
        public FloatAnimation(string propertyName, float start, float end, float duration)
            : base(duration)
        {
            this.propertyName = propertyName;
            this.start = start; this.end = end;
        }

        PropertyInfo pinfo;
        string propertyName;
        float start, end, delta;

        public override void InitializeWithTarget(object target)
        {
            if (target == null) return;
            Type t = target.GetType();
            pinfo = t.GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetProperty);
            delta = ((end - start) / Duration);
        }

        public override void UpdateWithTarget(object target, float time)
        {
            if (pinfo == null) return;
            float value = (start + delta * time);
            pinfo.SetValue(target, value, null);
        }

        public override Task Clone()
        {
            return new FloatAnimation(propertyName, start, end, Duration);
        }

        public override Task Reverse()
        {
            return new FloatAnimation(propertyName, end, start, Duration);
        }
    }
}
